package org.ricks.db;

import org.ricks.utils.ReflectUtil;

import java.lang.reflect.Field;
import java.math.BigDecimal;

/**
 * @author chenwei
 * @Title:
 * @Package
 * @Description:
 * @date 2020/12/2319:49
 */
public class BeanWrapper {
    private Object object;

    public BeanWrapper(Object var1) {
        this.object = var1;
    }

    private Field getFields(Class var1, String var2) {
        Field var3 = null;

        try {
            var3 = var1.getDeclaredField(var2);
        } catch (Exception var9) {
            Field[] var5 = var1.getDeclaredFields();
            int var6 = var5.length;

            for(int var7 = 0; var7 < var6; ++var7) {
                Field var8 = var5[var7];
                if (var8.getName().equalsIgnoreCase(var2)) {
                    var3 = var8;
                    break;
                }
            }

            if (var3 == null && var1.getSuperclass() != null && !var1.getSuperclass().equals(Object.class)) {
                var3 = this.getFields(var1.getSuperclass(), var2);
            }
        }

        return var3;
    }

    public void setPropertyValue(String var1, Object var2) {
        Field var3 = null;

        try {
            Class var4 = this.object.getClass();
            var3 = this.getFields(var4, var1);
            if (var3 != null) {
//                if (var3.getAnnotation(NotSaved.class) != null) {
//                    return;
//                }

                ReflectUtil.setAccessible(var3);
                var3.set(this.object, var2);
            }
        } catch (Exception var8) {
            if (var3 != null && var2 instanceof BigDecimal) {
                try {
                    String var5 = var3.getType().getName().toLowerCase();
                    byte var6 = -1;
                    switch(var5.hashCode()) {
                        case -1325958191:
                            if (var5.equals("double")) {
                                var6 = 3;
                            }
                            break;
                        case 104431:
                            if (var5.equals("int")) {
                                var6 = 1;
                            }
                            break;
                        case 3327612:
                            if (var5.equals("long")) {
                                var6 = 0;
                            }
                            break;
                        case 97526364:
                            if (var5.equals("float")) {
                                var6 = 2;
                            }
                    }

                    switch(var6) {
                        case 0:
                            ReflectUtil.setAccessible(var3);
                            var3.set(this.object, ((BigDecimal)var2).longValue());
                            break;
                        case 1:
                            ReflectUtil.setAccessible(var3);
                            var3.set(this.object, ((BigDecimal)var2).intValue());
                            break;
                        case 2:
                            ReflectUtil.setAccessible(var3);
                            var3.set(this.object, ((BigDecimal)var2).floatValue());
                            break;
                        case 3:
                            ReflectUtil.setAccessible(var3);
                            var3.set(this.object, ((BigDecimal)var2).doubleValue());
                    }
                } catch (Exception var7) {
                }
            } else {
                var8.printStackTrace();
            }
        }

    }

    public Object getPropertyValue(String var1) {
        Object var2 = null;

        try {
            Class var3 = this.object.getClass();
            Field var4 = this.getFields(var3, var1);
            ReflectUtil.setAccessible(var4);
            var2 = var4.get(this.object);
        } catch (Exception var5) {
        }

        return var2;
    }
}
